package cn.iocoder.yudao.module.contract.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.service.order.OrderService;
import cn.iocoder.yudao.module.contract.webservice.ContractWebService;
import cn.iocoder.yudao.module.contract.webservice.ZSD_JS_010;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author warrior
 **/
@Component
@Slf4j
public class OrderShippingStatusJob implements JobHandler {

    @Autowired
    private OrderService orderService;

    @Resource
    private ContractWebService contractWebService;


    @Override
    public String execute(String param) throws Exception {
        //获取未发货状态的合同订单
        List<OrderDO> orderDOS = orderService.listUnShippingOrders();
        orderDOS.stream().forEach(orderDO -> {
            ZSD_JS_010[] zsdJs010s = contractWebService.orderShippingStatus(orderDO.getVbeln());
            ZSD_JS_010 zsdJs010 = zsdJs010s[0];
            String zstate = zsdJs010.getZSTATE();
            String wadatIst = zsdJs010.getWADAT_IST();
            //订单完成状态
            String lfgsk = zsdJs010.getLFGSK();
            //订单完成状态描述
            String lfgskTxt = zsdJs010.getLFGSK_TXT();
            log.info("---vbeln:{},zstate:{},wadatIst:{}----->>>",orderDO.getVbeln(),zstate,wadatIst);
            if("已发货".equals(zstate)){
                //更新订单发货状态和最早发货时间
                orderService.updateOrderShippingStatus(orderDO.getVbeln(),wadatIst,lfgsk,lfgskTxt);
            }
        });
        return null;
    }
}
